package com.ruoyi.experiment.service.impl;

import java.util.List;

import com.ruoyi.experiment.domain.FirewallRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.FirewallStrategyMapper;
import com.ruoyi.experiment.domain.FirewallStrategy;
import com.ruoyi.experiment.service.IFirewallStrategyService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 防火墙策略Service业务层处理
 * 
 * @author 邓鑫
 * @date 2023-07-28
 */
@Service
public class FirewallStrategyServiceImpl implements IFirewallStrategyService 
{
    @Resource
    private FirewallStrategyMapper firewallStrategyMapper;

    /**
     * 查询防火墙策略
     * 
     * @param firewallStrategyId 防火墙策略主键
     * @return 防火墙策略
     */
    @Override
    public FirewallStrategy selectFirewallStrategyByFirewallStrategyId(Long firewallStrategyId)
    {
        return firewallStrategyMapper.selectFirewallStrategyByFirewallStrategyId(firewallStrategyId);
    }

    /**
     * 查询防火墙策略列表
     * 
     * @param firewallStrategy 防火墙策略
     * @return 防火墙策略
     */
    @Override
    public List<FirewallStrategy> selectFirewallStrategyList(FirewallStrategy firewallStrategy)
    {
        return firewallStrategyMapper.selectFirewallStrategyList(firewallStrategy);
    }

    /**
     * 新增防火墙策略
     * 
     * @param firewallStrategy 防火墙策略
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFirewallStrategy(FirewallStrategy firewallStrategy)
    {
        int flag=firewallStrategyMapper.insertFirewallStrategy(firewallStrategy);
        //插入策略规则表
        firewallStrategyMapper.insertFirewallStrategyRule(firewallStrategy.getRuleValue(),firewallStrategy.getFirewallStrategyId());
        return flag;
    }

    /**
     * 修改防火墙策略
     * 
     * @param firewallStrategy 防火墙策略
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFirewallStrategy(FirewallStrategy firewallStrategy)
    {
        //原来的规则列表
        List<FirewallRule> ruleList = firewallStrategy.getFirewallRuleList();
        Long[] ruleValue = firewallStrategy.getRuleValue();
        Long fsID = firewallStrategy.getFirewallStrategyId(); //策略id
        if(ruleValue.length>ruleList.size()){
            //增加
            //插入策略规则表
            for (Long aLong : ruleValue) {
                //判断是否在里面
                if(firewallStrategyMapper.queryStateRuleID(aLong,fsID)==null){
                    firewallStrategyMapper.insertStrategyRule(aLong,fsID);
                }
            }
        }else if(ruleValue.length<ruleList.size()){
            //减少
            for (FirewallRule rule : ruleList) {
                boolean flag=true;
                for (Long aLong : ruleValue) {
                    if(aLong==rule.getFirewallRuleId()){
                        flag=false;
                        break;
                    }
                }
                if(flag){
                    //删除策略规则表
                    firewallStrategyMapper.deleteStraRule(rule.getFirewallRuleId(),fsID);
                }
            }
        }
        return firewallStrategyMapper.updateFirewallStrategy(firewallStrategy);
    }

    /**
     * 批量删除防火墙策略
     * 
     * @param firewallStrategyIds 需要删除的防火墙策略主键
     * @return 结果
     */
    @Override
    public int deleteFirewallStrategyByFirewallStrategyIds(Long[] firewallStrategyIds)
    {
        return firewallStrategyMapper.deleteFirewallStrategyByFirewallStrategyIds(firewallStrategyIds);
    }

    /**
     * 删除防火墙策略信息
     * 
     * @param firewallStrategyId 防火墙策略主键
     * @return 结果
     */
    @Override
    public int deleteFirewallStrategyByFirewallStrategyId(Long firewallStrategyId)
    {
        return firewallStrategyMapper.deleteFirewallStrategyByFirewallStrategyId(firewallStrategyId);
    }

    /**
     * 不带参数获取策略列表
     */
    @Override
    public List<FirewallStrategy> getList() {
        return firewallStrategyMapper.getList();
    }
}
